package com.shb.jutils.commons;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.PrintQuality;

/**
 * 调用打印机工具类
 * 
 * @author shb
 * 
 */
public class PrinterUtil {
	/**
	 * 打印文件
	 * 
	 * @param file
	 */
	public static void printFile(File file) {
		// 设置打印格式
		DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

		// 定位打印服务
		PrintService ptService = PrintServiceLookup.lookupDefaultPrintService();

		// 创建打印作业
		DocPrintJob job = ptService.createPrintJob();
		try {
			InputStream fis = new FileInputStream(file);
			DocAttributeSet das = new HashDocAttributeSet();
			// 设置打印属性
			PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
			pras.add(OrientationRequested.PORTRAIT);
			pras.add(new Copies(1));
			pras.add(PrintQuality.HIGH);

			das.add(new MediaPrintableArea(0, 0, 4, 6, MediaPrintableArea.INCH));

			Doc doc = new SimpleDoc(fis, flavor, das);
			job.print(doc, pras);

			fis.close();
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (PrintException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 打印图片
	 * @param fis
	 */
	public static void printJPG(FileInputStream fis) {
		// 设置打印格式
		DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;

		// 定位打印服务
		PrintService ptService = PrintServiceLookup.lookupDefaultPrintService();

		// 创建打印作业
		DocPrintJob job = ptService.createPrintJob();
		try {
			DocAttributeSet das = new HashDocAttributeSet();
			// 设置打印属性
			PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
			pras.add(OrientationRequested.PORTRAIT);
			pras.add(new Copies(1));
			pras.add(PrintQuality.HIGH);

			das.add(new MediaPrintableArea(0, 0, 4, 6, MediaPrintableArea.INCH));

			Doc doc = new SimpleDoc(fis, flavor, das);
			job.print(doc, pras);

			fis.close();
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (PrintException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) throws FileNotFoundException {
		PrinterUtil.printJPG(new FileInputStream(new File("c:\\31.jpg")));
	}
}
